package com.gouaixin.bee.beeframework2.extra.html;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.gouaixin.bee.beeframework2.R;

public class WebViewActivity2 extends Activity {
    Context context=WebViewActivity2.this;



	private WebView contentWebView = null;
	private TextView msgView = null;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webviewdemo2);
		contentWebView = (WebView) findViewById(R.id.webview);
		msgView = (TextView) findViewById(R.id.msg);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(btnClickListener);



        String file_name="huodong01.html";
        loadAssetsHtml(context, contentWebView, file_name,"demo");
	}

    /***
     * 加载本地Html文件的方法
     * @param context
     * @param contentWebView
     * @param file_name
     * @param js_name
     */
    private void loadAssetsHtml(Context context, WebView contentWebView, String file_name,String js_name) {
        // 启用javascript
        contentWebView.getSettings().setJavaScriptEnabled(true);
        // 从assets目录下面的加载html
        contentWebView.loadUrl("file:///android_asset/html/"+file_name);
        contentWebView.addJavascriptInterface(this, js_name);
    }


    @JavascriptInterface
    public void clickOnAndroid() {
        Log.e("Demo","js调用了java函数");
        Toast.makeText(this, "js调用了java函数", Toast.LENGTH_SHORT).show();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                msgView.setText(msgView.getText() + "\njs调用了java函数");

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    OnClickListener btnClickListener = new OnClickListener() {
		public void onClick(View v) {
			// 无参数调用
			contentWebView.loadUrl("javascript:javacalljs()");
			// 传递参数调用
			contentWebView.loadUrl("javascript:javacalljswithargs(" + "'hello world'" + ")");
		}
	};


    @JavascriptInterface
	public void startFunction() {
		Toast.makeText(this, "js调用了java函数", Toast.LENGTH_SHORT).show();
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				msgView.setText(msgView.getText() + "\njs调用了java函数");

			}
		});
	}

    @JavascriptInterface
	public void startFunction(final String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				msgView.setText(msgView.getText() + "\njs调用了java函数传递参数：" + str);

			}
		});
	}
}